<template>
  <Table
    :columns="tableColumns"
    default-expand-all
    node-key="id"
    :data="dataList"
    :loading="loading"
    :pagination="{
      total
    }"
    @register="tableRegister"
  />
</template>
<script setup lang="tsx">
import { PropType, reactive, ref, unref } from 'vue'
import { ElTag } from 'element-plus'
import { orderGoodsDetailApi as requestApi } from '@/api/api'
import { useTable } from '@/hooks/web/useTable'
import { useI18n } from '@/hooks/web/useI18n'
import { Table, TableColumn } from '@/components/Table'
import { BaseButton } from '@/components/Button'

const { t } = useI18n()

const props = defineProps({
  currentRow: {
    type: Object as PropType<any>,
    default: () => null
  }
})
const delId = ref('')
const { tableRegister, tableState, tableMethods } = useTable({
  fetchDataApi: async (params: any) => {
    const res = await requestApi.page({
      ...params,
      order_id: props.currentRow.id
    })
    console.log(res)
    return {
      list: (res.data.rows || []).map((el) => {
        return {
          ...el
        }
      }),
      total: res.data.count
    }
  },
  fetchDelApi: async () => {
    console.log(delId.value)
    const res = await requestApi.del(unref(delId))
    return !!res
  }
})

const { dataList, loading, total } = tableState
const { getList, delList } = tableMethods

const tableColumns = reactive<TableColumn[]>([
  {
    field: 'index',
    label: t('active.index'),
    type: 'index'
  },
  {
    field: 'orderGood.order_no',
    label: '订单号'
  },
  {
    field: 'good.name',
    label: '商品'
  },
  {
    field: 'count',
    label: '数量'
  },
  {
    field: 'status',
    label: '状态',
    slots: {
      default: (data: any) => {
        return (
          <>
            <ElTag type={data.row.status === 0 ? 'danger' : 'success'}>
              {data.row.status === 1 ? '已确认' : '待确认'}
            </ElTag>
          </>
        )
      }
    }
  },
  {
    field: 'action',
    label: t('userDemo.action'),
    width: 180,
    slots: {
      default: (data: any) => {
        const row = data.row
        return (
          <>
            {row.status === 0 ? (
              <BaseButton type="primary" onClick={() => confirmAction(row)}>
                确认
              </BaseButton>
            ) : null}
            {row.status === 0 ? (
              <BaseButton type="danger" onClick={() => delData(row)}>
                {t('exampleDemo.del')}
              </BaseButton>
            ) : null}
          </>
        )
      }
    }
  }
])

const confirmAction = (row: any) => {}

const delData = async (row: any | null) => {
  delId.value = row.id
  await delList()
}
</script>
